#include "inoutstockmodel.h"

InOutStockModel::InOutStockModel():BaseModel ()
{

}

int InOutStockModel::Table_Query(char *sql)
{
    char *errmsg;//接受错误信息
    char **datares;//接受查询结果集
    int res,row,col;
    res=sqlite3_get_table(this->psqlite,sql,&datares,&row,&col,&errmsg);
    if(res==SQLITE_OK)
    {
        qDebug()<<"search ok";
    }
    else
    {
        qDebug()<<"search no";

    }
    return row;
}

void InOutStockModel::Table_Delete(char *sql)
{
    char *errmsg;
    int res;
    res=sqlite3_exec(this->psqlite,sql,nullptr,nullptr,&errmsg);
    if(res==SQLITE_OK)
    {
        qDebug()<<"search ok";
    }
    else
    {
        qDebug()<<"search no";
    }
}

int InOutStockModel::Table_Inserter(char *sql)
{
    char *errmsg;//接受错误信息
    int res;
    res=sqlite3_exec(this->psqlite,sql,nullptr,nullptr,&errmsg);
    if(res==SQLITE_OK)
    {
        qDebug()<<"inserte ok";
        return 1;
    }
    else
    {
        qDebug()<<"inserte no";

    } return 0;
}

int InOutStockModel::Table_Modifier(char *sql)
{
    char *errmsg;//接受错误信息
    int res;
    res=sqlite3_exec(this->psqlite,sql,nullptr,nullptr,&errmsg);
    if(res==SQLITE_OK)
    {
        qDebug()<<"inserte ok";
        return 1;
    }
    else
    {
        qDebug()<<"inserte no";
        return 0;
    }
}

QVector<InOutStockData> InOutStockModel::getInOutStock(char *sql)
{
    InOutStockData *stock=nullptr;
    InOutStock.resize(100);//初始化
    //adduser.fill(nullptr);//置零
    char *errmsg;//接受错误信息
    char **datares;//接受查询结果集
    int res,row,col;
    //qDebug()<<"0";
    res=sqlite3_get_table(this->psqlite,sql,&datares,&row,&col,&errmsg);
    qDebug()<<"查询用户数据结束";
     if (row>0)
    {
         //qDebug()<<"row="<<row;
         //把所有数据全都放入adduser中
        for(int i=0;i<row;i++)
        {

          stock=new InOutStockData(atoi(datares[(i+1)*5]),atoi(datares[(i+1)*5+1]),atoi(datares[(i+1)*5+2]),atoi(datares[(i+1)*5+3]),datares[(i+1)*5+4]);
         this->InOutStock[i]=stock;
         //qDebug()<<"输出用户数据结束";
        }

    }
     return InOutStock;
}

int InOutStockModel::getRow(char *sql)
{
    StockData *stock=nullptr;
    InOutStock.resize(100);//初始化
    char *errmsg;//接受错误信息
    char **datares;//接受查询结果集
    int res,row,col;
    //qDebug()<<"0";
    res=sqlite3_get_table(this->psqlite,sql,&datares,&row,&col,&errmsg);
    qDebug()<<"row="<<row;
    return row;
}
